package com.example.API;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class JDK8LocalDateTime {
    public static void main(String[] args) {
        /*LocalDateTime now = LocalDateTime.now();
        System.out.println(now);    //2024-08-18T15:20:51.754377400

        LocalDateTime localDateTime = LocalDateTime.of(2024, 8, 18, 14, 11, 1);
        System.out.println(localDateTime);  //2024-08-18T14:11:01
        */

        //getmethod();
        //tomethod();
        //formatmethod();
        //plusAndminus();
        //Withmethod();

        LocalDate localDate1 = LocalDate.of(2024, 8, 8);
        LocalDate localDate2 = LocalDate.of(2023, 1, 11);
        Period period = Period.between(localDate2, localDate1);
        System.out.println(period);     //P1Y6M28D
        System.out.println(period.getYears());  //1
        System.out.println(period.getMonths()); //6
        System.out.println(period.getDays());   //28
        System.out.println(period.toTotalMonths()); //18

        LocalDateTime localDateTime = LocalDateTime.of(2022, 11, 11, 12, 13, 14);
        LocalDateTime localDateTime1 = LocalDateTime.of(2024, 11, 11, 13, 14, 15);
        Duration duration = Duration.between(localDateTime, localDateTime1);
        System.out.println(duration);       //PT17545H1M1S
        System.out.println(duration.toSeconds());     //63162061
        System.out.println(duration.toMillis());      //63162061000
        System.out.println(duration.toNanos());       //63162061000000000

    }

    private static void Withmethod() {
        LocalDateTime localDateTime = LocalDateTime.of(2024, 8, 8, 11, 12, 13);
        LocalDateTime newlocalDateTime = localDateTime.withYear(2022);
        System.out.println(newlocalDateTime);       //2022-08-08T11:12:13

        LocalDateTime newlocalDateTime1 = localDateTime.withMonth(2);
        System.out.println(newlocalDateTime1);      //2024-02-08T11:12:13
    }

    private static void plusAndminus() {
        LocalDateTime localDateTime = LocalDateTime.of(2024, 8, 8, 11, 12, 13);
        LocalDateTime newlocalDateTime = localDateTime.plusDays(1);
        System.out.println(newlocalDateTime);       //2024-08-09T11:12:13

        LocalDateTime newlocalDateTime1 = localDateTime.plusDays(-1);
        System.out.println(newlocalDateTime1);      //2024-08-07T11:12:13


        LocalDateTime localDateTime1 = LocalDateTime.of(2024, 8, 8, 11, 12, 13);
        LocalDateTime newlocalDateTime2 = localDateTime1.minusYears(1);
        System.out.println(newlocalDateTime2);      //2023-08-08T11:12:13

        LocalDateTime newlocalDateTime3 = localDateTime1.minusYears(-1);
        System.out.println(newlocalDateTime3);      //2025-08-08T11:12:13
    }

    private static void tomethod() {
        LocalDateTime localDateTime = LocalDateTime.of(2024, 8, 18, 23, 59, 59);
        LocalDate localDate = localDateTime.toLocalDate();
        System.out.println(localDate);      //2024-08-18

        LocalTime localTime = localDateTime.toLocalTime();
        System.out.println(localTime);      //23:59:59
    }

    private static void getmethod() {
        LocalDateTime localDateTime = LocalDateTime.of(2024, 8, 18, 23, 59, 59);

        int year = localDateTime.getYear();
        System.out.println("年份为" + year);

        int monthValue = localDateTime.getMonthValue();
        System.out.println("月份为" + monthValue);

        int dayOfMonth = localDateTime.getDayOfMonth();
        System.out.println("日期为" + dayOfMonth);

        int dayOfYear = localDateTime.getDayOfYear();
        System.out.println("一年中的" + dayOfYear + "天");    //一年中的231天

        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        System.out.println("星期为" + dayOfWeek);    //星期为SUNDAY

        int minute = localDateTime.getMinute();
        System.out.println("分钟为" + minute);

        int hour = localDateTime.getHour();
        System.out.println("小时为" + hour);
    }

    private static void formatmethod() {
        //把一个LocalDateTime格式化成为一个字符串
        LocalDateTime localDateTime = LocalDateTime.of(2024, 8, 8, 8, 8, 8);
        System.out.println(localDateTime);  //2024-08-08T08:08:08
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = localDateTime.format(pattern);
        System.out.println(format);         //2024-08-08 08:08:08

        //把一个日期字符串解析成为一个LocalDateTime对象
        String s = "2024年08月08日 08:08:08";
        DateTimeFormatter pattern1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        LocalDateTime parse = localDateTime.parse(s, pattern1);
        System.out.println(parse);          //2024-08-08T08:08:08
    }
}
